using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YayiMgr.Ability.Docking.DictionaryApp;
using YayiMgr.Ability.Docking.DictionaryApp.Dto;
using YayiMgr.Domain.SystemBase;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace YayiMgr.Ability.DictionaryApp
{
    public class DictionaryTypeServicer: AbilityServicer, IDictionaryTypeServicer
    {
        public IRepository<DictionaryType> DicItemRepo { get; set; }
        public async Task<List<DictionaryTypeDto>> GetAllDictionaryTypes()
        {
            List<DictionaryType> items = await DicItemRepo.GetListAsync();
            var itemDots = ModelMapper.Map<List<DictionaryType>, List<DictionaryTypeDto>>(items);
            return itemDots;
        }

        public async Task<DictionaryTypeDto> GetAllDictionaryTypesById(Guid Id)
        {
            DictionaryType item = await DicItemRepo.GetAsync(Id);
            return ModelMapper.Map<DictionaryType, DictionaryTypeDto>(item);
        }

        public async Task<DictionaryTypeDto> AddDictionaryType(DictionaryTypeCreateDto dto)
        {
            var item = ModelMapper.Map<DictionaryTypeCreateDto, DictionaryType>(dto);
            item.Id = SortGuid.NewGuid();
            item = await DicItemRepo.InsertAsync(item);
            var itemDto = ModelMapper.Map<DictionaryType, DictionaryTypeDto>(item);
            return itemDto;
        }

        public async Task<DictionaryTypeDto> UpdateDictionaryType(Guid Id, DictionaryTypeUpdateDto dto)
        {
            DictionaryType item = await DicItemRepo.GetAsync(Id);
            item = ModelMapper.Map<DictionaryTypeUpdateDto, DictionaryType>(dto);
            item = await DicItemRepo.UpdateAsync(item);
            DictionaryTypeDto dto1 = ModelMapper.Map<DictionaryType,DictionaryTypeDto>(item);
            return dto1;
        }

        public async Task DeleteDictionaryType(Guid Id)
        {
            await DicItemRepo.DeleteAsync(Id);
        }        
    }
}